import { pic, content, bg, content2, title_,pc_ } from '../../utils/canvas/index.js';
import { Authorize, getCanvasPic } from '../../utils/util.js';


// pages/canvas/canvas.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    canvas_form: {},
    pic: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531214138209&di=9bdab820f972680b8845217c61b894d4&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D325440579%2C2874869038%26fm%3D214%26gp%3D0.jpg'
  },
  
  // 
  handledButton: function (e) {
    
    // this.setCanvas();
    let _getCanvasPic = new getCanvasPic(bg, "secondCanvas");
    var authorize = Authorize("scope.writePhotosAlbum");// 封装权限获取
    // 
    authorize.then((res) => {// 权限获取成功
      _getCanvasPic.then(res => {// canvas图片路径生成成功
        console.log(res, 'pic');
        wx.saveImageToPhotosAlbum({// 保存图片
          filePath: res.tempFilePath,
          success: function () {
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 2000
            })
          }
        });
      });
    });
    
  },
  setCanvas : () => {
    const canvas_ = wx.createCanvasContext("secondCanvas");
    console.log(canvas_);
    canvas_.save();// 初始状态保存
    bg.paint(canvas_);
    content.paint(canvas_);
    content2.paint(canvas_);
    pic.paint(canvas_);
    pc_.paint(canvas_);
    title_.paint(canvas_);
    console.log(bg.left);
    canvas_.draw();// 绘制

  },
  // 
  onShow: function () {
    const that = this;
    // let pic = '';
    // this.setCanvas();
    // wx.getImageInfo({
    //   src: this.data.pic,
    //   success: function (res) {
    //     console.log(res);
    //     that.setCanvas();
    //   }
    // })
    wx.downloadFile({
      url: this.data.pic,
      success: function (sres) {
        pic.setAttr({
          pic_path: sres.tempFilePath
        });
        that.setCanvas();
        that.setCanvas();// 手机端 文字需要二次渲染
      }, fail: function (fres) {
        console.log(fres);
      }
    })
  },
})